<?php

namespace App\Listeners;

use Dingo\Api\Event\ResponseWasMorphed;

class PaginationTransformerListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    public function handle(ResponseWasMorphed $event)
    {
        if (isset($event->content['meta']['pagination'])) {
            @list($sortBy, $descending) = explode(',', request()->get('sortBy'));
            $sortBy = $sortBy ?: 'id';
            $descending = (strtolower($descending) === 'desc') ?: false;
            $event->content['meta']['pagination']['sortBy'] = $sortBy;
            $event->content['meta']['pagination']['descending'] = $descending;
        }
    }
}